<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" name="referrer" content="no-referrer">
    <title>个人中心</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!-- 引入axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <!-- 引入Vue -->
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
    <!-- import Element UI -->
    <script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
    <link rel="stylesheet" href="css/Myself.css">
</head>
<body>
<div id="app">
    <el-container>
        <el-header height="150px">
            <div style="width: 1400px;margin: 0 auto">
                <img src="../imgs/logo.png" width="300px">
                <a href="">首页</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">热点资讯</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">商家入驻</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">社会招聘</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">校园招聘</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">帮助</a>
                <el-divider direction="vertical"></el-divider>
                <span style="float: right;font-size: 15px;margin-top: 35px">
                    欢迎回来<a href="JavaScript:void(0)" style="text-decoration: none">{{nickname}}</a>
                    &nbsp&nbsp&nbsp
                    <a href="JavaScript:void(0)" style="text-decoration: none" @click="logout">退出登录</a>
                </span>
            </div>
        </el-header>
        <el-container>
            <el-aside width="200px">
                <el-menu
                        default-active="3"
                        class="el-menu-vertical-demo"
                        :unique-opened="true"
                        @Select="handleSelect"
                        @open="handleOpen"
                        @close="handleClose">
                    <el-menu-item index="1">
                        <i class="el-icon-s-home"></i>
                        <span slot="title">我的</span>
                    </el-menu-item>
                    <el-menu-item index="2">
                        <i class="el-icon-s-order"></i>
                        <span slot="title">我的订单</span>
                    </el-menu-item>
                    <el-menu-item index="3">
                        <i class="el-icon-shopping-cart-full"></i>
                        <span slot="title">我的购物车</span>
                    </el-menu-item>
                    <el-menu-item index="4">
                        <i class="el-icon-setting"></i>
                        <span slot="title">修改密码</span>
                    </el-menu-item>
                    <el-submenu index="5">
                        <template slot="title">
                            <i class="el-icon-map-location"></i>
                            <span>地址管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="5-1">新增地址</el-menu-item>
                            <el-menu-item index="5-2">地址列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <el-table :data="cartList">
                    <el-table-column type="index" label="编号" width="180"></el-table-column>
                    <el-table-column prop="spuId" label="商品ID" width="180"></el-table-column>
                    <el-table-column prop="title" label="商品名称" width="180"></el-table-column>
                    <el-table-column prop="price" label="商品价格" width="180"></el-table-column>
                    <el-table-column prop="num" label="商品数量" width="180">
                        <template slot-scope="scope">
                            <el-input-number v-model="scope.row.num" size="mini"
                                             @change="handleChange" :min="1" :max="500"></el-input-number>
                        </template>
                    </el-table-column>
                    <el-table-column prop="img" label="商品图片">
                        <template slot-scope="scope">
                            <img :src="scope.row.img" alt="" width="100">
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                       <template slot-scope="scope">
                           <el-button type="danger" circle class="el-icon-delete" @click="deleteCart(scope.row)"></el-button>
                       </template>
                        <el-button type="success" circle class="el-icon-shopping-bag-1" @click="buy"></el-button>
                    </el-table-column>
                </el-table>
            </el-main>
        </el-container>
    </el-container>
</div>
</body>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                nickname: localStorage.getItem('nickname'),
                cartList: [],
                order: {
                    userId: localStorage.getItem("userId"),
                    spuId: "",
                    title: "",
                    CountPrice: "",
                    num: "",
                    img: "",
                }
            }
        },
        methods: {
            handleChange(value) {
                console.log(value);
            },
            handleOpen(key, keyPath) {
                console.log("handleOpen = " + key, keyPath);
            },
            handleClose(key, keyPath) {
                console.log("handleClose = " + key, keyPath);
            },
            handleSelect(key, keyPath) {
                console.log(key)
                if (key === '1'){
                    location.href = 'Myself.html'
                } else if (key === '2'){
                    location.href = 'Order.html'
                } else if (key === '4'){
                    location.href = 'ChangePassword.html'
                } else if (key === '5-1'){
                    location.href = 'addressAdd.html'
                }else if (key === '5-2'){
                    location.href = 'AddressList.html'
                }
            },
            logout() {
                location.href = '../login.html'
            },
            buy() {
                console.log("buy")

            },
            deleteCart(row) {
                console.log("deleteCart = " + row.spuId)
                console.log("userId = "+localStorage.getItem('userId'))
                axios.get("/cart/delete", {
                    params: {
                        userId: localStorage.getItem('userId'),
                        spuId: row.spuId
                    }
                }).then(function (response) {
                    let responseBody = response.data;
                    if (responseBody.state === 20000) {
                        v.$message.success("删除成功")
                        axios.get("/cart/list", {
                            params: {
                                userId: localStorage.getItem('userId')
                            }
                        }).then(function (response) {
                            let responseBody = response.data;
                            if (responseBody.state === 20000) {
                                console.log("responseBody.data=" + responseBody.data)
                                v.cartList = responseBody.data;
                            }
                        })
                    }
                })
            }
        },
        created: function () {
            console.log("userID = " + location.search)
            axios.get("/cart/list", {
                params: {
                    userId: localStorage.getItem('userId')
                }
            }).then(function (response) {
                    let responseBody = response.data;
                    if (responseBody.state === 20000) {
                        console.log("responseBody.data=" + responseBody.data)
                        v.cartList = responseBody.data;
                    }
                })
        }
    })
</script>
</html>